//
//  DepositPaymentController.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/6/5.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJDepositPaymentController.h"
#import "ZJSuccessfulEnrollmentController.h"

@interface ZJDepositPaymentController ()<UIActionSheetDelegate>{
    NSInteger type;
    ZJSuccessfulEnrollmentController *_vc ;
}
@property (weak, nonatomic) IBOutlet UIImageView *mineImg; //商品图片
@property (weak, nonatomic) IBOutlet UILabel *mainTitle; //商品名字
@property (weak, nonatomic) IBOutlet UILabel *payMentTitle; //定金
@property (weak, nonatomic) IBOutlet UIButton *netBtn;

@property (strong, nonatomic)UIAlertController *alertController;
@end

@implementation ZJDepositPaymentController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
   _vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJSuccessfulEnrollmentController"];
    _vc.isBackRoot = YES;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

-(void)initView{
    self.title = @"支付报名费";
    [self.netBtn setBackgroundColor:[Utils colorWithHexString:allConstHexColor]];
    ZJProjectDetailModel * model = GET_PROJECTINFO;
    self.mainTitle.text = model.project_name;
    self.mineImg.image = [UIImage imageWithContentsOfFile:[Utils imgPath:@"tqbm_pic_cg@3x.png"]];
    self.payMentTitle.textColor = [Utils colorWithHexString:allConstHexColor];
    self.payMentTitle.text =[NSString stringWithFormat:@"%@元", model.deposit_money?:@"0"];
}

- (IBAction)nextBtn:(UIButton *)sender {
    
    self.alertController = [UIAlertController alertControllerWithTitle:@"选择支付方式"
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleActionSheet];
    //取消:style:UIAlertActionStyleCancel
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [_alertController addAction:cancelAction];
    

    UIAlertAction *moreAction = [UIAlertAction actionWithTitle:@"支付宝支付" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        type = 1;
        [self sendRequest];
    }];
    [_alertController addAction:moreAction];
 
//    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"微信支付" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//        
//        type = 2;
//        [self sendRequest];
//    }];
//    [_alertController addAction:OKAction];
    
    [self presentViewController:_alertController animated:YES completion:nil];
    
    
}

-(void)sendRequest{

    [HUD showLoading:@"支付中..."];
    NSDictionary *parameter = [LRNetwork requestParameter:@{@"pay_type":@(type)}];
    NSString *url = [LRNetwork requestURL:@"cgi/deposit.php"];
    
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
        
//        [self.navigationController pushViewController:vc animated:YES];
        // NOTE: 调用支付结果开始支付
        [[AlipaySDK defaultService] payOrder:returnValue[@"info"] fromScheme:@"zj100HCJT" callback:^(NSDictionary *resultDic) {
            NSString *strTitle = @"支付结果";
            __block NSString *strMsg = @"";
            NSLog(@"reslut = %@",resultDic);
            NSString * resultStatus = resultDic[@"resultStatus"];
            if ([resultStatus isEqualToString:@"9000"]) {
                strMsg = @"支付成功!";

                [self.navigationController pushViewController:_vc animated:YES];
            }else if ([resultStatus isEqualToString:@"4000"]){
                strMsg = @"支付失败!";
            }else if ([resultStatus isEqualToString:@"5000"]){
                strMsg = @"重复请求!";
            }else if ([resultStatus isEqualToString:@"6001"]){
                strMsg = @"用户中途取消!";
            }else if ([resultStatus isEqualToString:@"6002"]){
                strMsg = @"网络连接出错!";
            }else if ([resultStatus isEqualToString:@"6004"]){
                strMsg = @"支付结果未知(请联系供应商)";
            }
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }];
        [HUD hide];
        
    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        [HUD showErrorMsg:errorMessage];
    } withFailureBlock:^{
        
    }];
}
@end
